package com.lb.shopguide.ui.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lb.shopguide.GuideApplication;
import com.lb.shopguide.R;
import com.lb.shopguide.entity.goods.SkuBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by kang on 2017/6/14.
 */

public class LbSkuDisplayLayout extends LinearLayout {


    public LbSkuDisplayLayout(Context context) {
        super(context);
    }

    public LbSkuDisplayLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
//        LayoutInflater.from(context).inflate(R.layout.layout_display_sku, this);
    }

    public void setData(Map<String, List<SkuBean>> map) {
        for (Map.Entry<String, List<SkuBean>> entry : map.entrySet()) {
            addView(entry);
        }
    }

    /**
     * 动态添加view
     *
     * @param entry
     */
    private void addView(Map.Entry<String, List<SkuBean>> entry) {

        float density = GuideApplication.getAppContext().getResources().getDisplayMetrics().density;

        ArrayList<String> sizeList = new ArrayList<>();
        ArrayList<String> sumList = new ArrayList<>();
        List<SkuBean> skuList = entry.getValue();
        for (SkuBean skuBean : skuList) {
            sizeList.add(skuBean.getSize());
            sumList.add("" + skuBean.getStoreSum());
        }

        View viewLine = new View(getContext());
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, 1);
        viewLine.setBackgroundColor(getResources().getColor(R.color.sg_common_line_bg_dark));
        addView(viewLine, params);

        LayoutParams paramsRelativeLayout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        RelativeLayout relativeLayout = new RelativeLayout(getContext());

        RelativeLayout.LayoutParams paramsTvColor = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        paramsTvColor.addRule(RelativeLayout.ALIGN_PARENT_LEFT | RelativeLayout.CENTER_VERTICAL);
        paramsTvColor.setMargins((int) (10 * density), 0, 0, 0);
        TextView tvColor = new TextView(getContext());
        tvColor.setText(entry.getKey());
        tvColor.setTextColor(getResources().getColor(R.color.sg_black));
        tvColor.setTextSize(12);
        tvColor.setGravity(Gravity.CENTER);
        relativeLayout.addView(tvColor, paramsTvColor);

        RelativeLayout.LayoutParams paramsLayoutSize = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        paramsLayoutSize.addRule(RelativeLayout.CENTER_IN_PARENT);
        LinearLayout layoutSize = new LinearLayout(getContext());
//        layoutSize.setBackgroundColor(getResources().getColor(R.color.yellow));
        layoutSize.setOrientation(VERTICAL);
        for (String key : sizeList) {
            addSize(layoutSize, key);
        }
        relativeLayout.addView(layoutSize, paramsLayoutSize);


        RelativeLayout.LayoutParams paramsLayoutSum = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        paramsLayoutSum.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        paramsLayoutSum.setMargins(0, 0, (int) (10 * density), 0);
        LinearLayout layoutSum = new LinearLayout(getContext());
//        layoutSum.setBackgroundColor(getResources().getColor(R.color.yellow));
        layoutSum.setOrientation(VERTICAL);
        for (String key : sumList) {
            addSum(layoutSum, key);
        }
        relativeLayout.addView(layoutSum, paramsLayoutSum);

        addView(relativeLayout, paramsRelativeLayout);
    }

    private void addSize(LinearLayout layout, String word) {
        float density = GuideApplication.getAppContext().getResources().getDisplayMetrics().density;
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(0, (int) (20 * density), 0, (int) (20 * density));
        TextView textView = new TextView(getContext());
        textView.setTextColor(getResources().getColor(R.color.sg_black));
        textView.setText(word);
        textView.setTextSize(12);
        textView.setGravity(Gravity.RIGHT);
        layout.addView(textView, params);
    }

    private void addSum(LinearLayout layout, String word) {
        float density = GuideApplication.getAppContext().getResources().getDisplayMetrics().density;
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(0, (int) (20 * density), 0, (int) (20 * density));
        TextView textView = new TextView(getContext());
        textView.setTextColor(getResources().getColor(R.color.sg_black));
        textView.setText(word + "件");
        textView.setTextSize(13);
//        textView.setGravity(Gravity.RIGHT);
        params.gravity = Gravity.RIGHT;
        layout.addView(textView, params);
    }
}
